package cn.iot.sys.service.impl;

import cn.iot.common.dto.ResultMsg;
import cn.iot.common.utils.StringUtils;
import cn.iot.sys.dao.SysElectricityUseDao;
import cn.iot.sys.entity.SysElectricityUse;
import cn.iot.sys.service.SysElectricityUseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysElectricityUseServiceImpl implements SysElectricityUseService {
    @Resource
    private SysElectricityUseDao sysElectricityUseDao;

    private Logger logger = LoggerFactory.getLogger(getClass());
    @Override
    public ResultMsg getSysElectricityUseList(Map<String, Object> map) {
        logger.info("SysElectricityUseServiceImpl getSysElectricityUseList map{}",map);
        String pageNum = (String) map.get("pageNum");
        int pageSize = Integer.parseInt((String) map.get("pageSize"));
        String code = (String) map.get("code");
        Map<String,Object> map1 = new HashMap<>();
        int pageNum1 = 1;
        if (pageNum != null){
            pageNum1 = Integer.parseInt(pageNum);
            int start = (pageNum1-1)*pageSize;
            map1.put("start",start);
        }
        if (StringUtils.isNotEmpty(code)){
            map1.put("deviceId",Integer.parseInt(code));
            pageNum1 = Integer.parseInt(pageNum);
            int start = (pageNum1-1)*pageSize;
            map1.put("start",start);
        }else {
            map1.put("deviceId","");
        }
        map1.put("pageSize",pageSize);
        List<SysElectricityUse> sysElectricityUseList = sysElectricityUseDao.getSysElectricityUseList(map1);
        int count = sysElectricityUseDao.getSysElectricityUseCount(map1);
        int pageCount = count%pageSize==0? count/pageSize:count/pageSize+1;
        logger.debug("sysElectricityUseList{},pageCount{}",sysElectricityUseList,pageCount);
        map.put("sysElectricityUseList",sysElectricityUseList);
        map.put("pageCount",pageCount);
        ResultMsg msg = new ResultMsg(200,"成功",map);
        return msg;
    }
}
